﻿#include "Configer.h"
#include <QSettings>
#include <QDebug>

void readBasicAuth(BasicAuth &basicAuth)
{
	QSettings iniReader(CONFIG_INI_PATH, QSettings::IniFormat);
	basicAuth.UserName = iniReader.value(USERNAME_INI_KEY).toString();
	basicAuth.PassWord = iniReader.value(PASSWORD_INI_KEY).toString();
}

void saveBasicAuth(BasicAuth basicAuth)
{
	QSettings iniWrite(CONFIG_INI_PATH, QSettings::IniFormat);
	iniWrite.setValue(USERNAME_INI_KEY, basicAuth.UserName);
	iniWrite.setValue(PASSWORD_INI_KEY, basicAuth.PassWord);
}

void readSSLParams(SSLParamer &sslParams)
{
	//读取ini文件
	QSettings iniReader(CONFIG_INI_PATH, QSettings::IniFormat);
	sslParams.CAFilePath = iniReader.value(CAFILE_INI_KEY).toString();
	sslParams.certFilePath = iniReader.value(CERTFILE_INI_KEY).toString();
	sslParams.certFileType = iniReader.value(CERTFILETYPE_INI_KEY).toString();
	sslParams.keyFilePath = iniReader.value(KEYFILE_INI_KEY).toString();
	sslParams.keyFileType = iniReader.value(KEYFILETYPE_INI_KEY).toString();
	sslParams.keyPassWord = QByteArray::fromBase64(iniReader.value(KEYPASSWORD_INI_KEY).toString().toLocal8Bit());
}

void saveSSLParams(SSLParamer sslParams)
{
	QSettings iniWrite(CONFIG_INI_PATH, QSettings::IniFormat);
	iniWrite.setValue(CAFILE_INI_KEY, sslParams.CAFilePath);
	iniWrite.setValue(CERTFILE_INI_KEY, sslParams.certFilePath);
	iniWrite.setValue(CERTFILETYPE_INI_KEY, sslParams.certFileType);
	iniWrite.setValue(KEYFILE_INI_KEY, sslParams.keyFilePath);
	iniWrite.setValue(KEYFILETYPE_INI_KEY, sslParams.keyFileType);
	iniWrite.setValue(KEYPASSWORD_INI_KEY, sslParams.keyPassWord);
}

QString readGlobalIniConfig(QString configFilePath, QString key)
{
	QSettings *configIni = new QSettings(configFilePath, QSettings::IniFormat);
	QString value = configIni->value(key).toString();
	if (key.toLower().contains("token"))
	{
		qInfo() << "read  ini ==>" << key << " : ******";
	}
	else
	{
		qInfo() << "read  ini ==>" << key << " : " << value;
	}
	delete configIni;
	return value;
}


QString readGlobalIniConfig(QString key)
{
	QSettings *configIni = new QSettings(CONFIG_INI_PATH, QSettings::IniFormat);
	QString value = configIni->value(key).toString();
	if (key.toLower().contains("token"))
	{
		qInfo() << "read  ini ==>" << key << " : ******";
	}
	else
	{
		qInfo() << "read  ini ==>" << key << " : " << value;
	}
	delete configIni;
	return value;
}

void writeGlobalIniConfig(QString configFilePath, QString key, QString value)
{
	QSettings *configIni = new QSettings(configFilePath, QSettings::IniFormat);
	if (key.toLower().contains("token"))
	{
		qInfo() << "write  ini ==>" << key << " : ******";
	}
	else
	{
		qInfo() << "write ini ==>" << key << " : " << value;
	}
	configIni->setValue(key, value);
	delete configIni;
}


void writeGlobalIniConfig(QString key, QString value)
{
	QSettings *configIni = new QSettings(CONFIG_INI_PATH, QSettings::IniFormat);
	if (key.toLower().contains("token"))
	{
		qInfo() << "write  ini ==>" << key << " : ******";
	}
	else
	{
		qInfo() << "write ini ==>" << key << " : " << value;
	}
	configIni->setValue(key, value);
	delete configIni;
}
